package index;

import heap.RID;
import storage.data.PageID;


/**
 * define (key, data) pair
 * 
 * @author zhangyu
 * 
 */
public class KeyDataEntry {

	public KeyClass key;

	public DataClass data;

	public KeyDataEntry(Integer key, PageID pageNo) {
		this.key = new IntegerKey(key);
		this.data = new IndexData(pageNo);
	}

	public KeyDataEntry(String key, PageID pageNo) {
		this.key = new StringKey(key);
		this.data = new IndexData(pageNo);
	}

	public KeyDataEntry(Integer key, RID rid) {
		this.key = new IntegerKey(key);
		this.data = new LeafData(rid);
	}

	public KeyDataEntry(KeyClass key, RID rid) {
		data = new LeafData(rid);
		if (key instanceof IntegerKey)
			this.key = new IntegerKey(((IntegerKey) key).getKey());
		else if (key instanceof FloatKey) 
			this.key = new FloatKey(((FloatKey) key).getKey());
		else if (key instanceof StringKey)
			this.key = new StringKey(((StringKey) key).getKey());
	}

	public KeyDataEntry(String key, RID rid) {
		this.key = new StringKey(key);
		this.data = new LeafData(rid);
	}

	public KeyDataEntry(KeyClass key, PageID pageNo) {

		data = new IndexData(pageNo);
		if (key instanceof IntegerKey)
			this.key = new IntegerKey(((IntegerKey) key).getKey());
		else if(key instanceof FloatKey)
			this.key = new FloatKey(((FloatKey) key).getKey());
		else if (key instanceof StringKey)
			this.key = new StringKey(((StringKey) key).getKey());
	}

	public KeyDataEntry(KeyClass key, DataClass data) {
		if (key instanceof IntegerKey) {
			this.key = new IntegerKey(((IntegerKey) key).getKey());
		} else if (key instanceof StringKey) {
			this.key = new StringKey(((StringKey) key).getKey());
		} else if (key instanceof FloatKey) {
			this.key = new FloatKey(((FloatKey) key).getKey());
		}

		if (data instanceof IndexData) {
			this.data = new IndexData(((IndexData) data).getData());

		} else if (data instanceof LeafData) {
			this.data = new LeafData(((LeafData) data).getData());
		}
	}

	public boolean equals(KeyDataEntry entry) {
		boolean st1, st2;
		// System.out.println("entry:"+entry.key+" "+entry.data+" "+"key:"+key+"
		// "+data);
		if (key instanceof IntegerKey) {
			st1 = ((IntegerKey) key).getKey().equals(
					((IntegerKey) entry.key).getKey());
		} else if (key instanceof FloatKey) {
			st1 = ((FloatKey) key).getKey()==((FloatKey) key).getKey();
		} else {
			st1 = ((StringKey) key).getKey().equals(
					((StringKey) entry.key).getKey());
		}
		if (data instanceof IndexData) {
			st2 = ((IndexData) data).getData().pageId == ((IndexData) entry.data)
					.getData().pageId;
		} else {
			st2 = ((RID) ((LeafData) data).getData())
					.equals(((RID) ((LeafData) entry.data).getData()));
			// System.out.println((RID) ((LeafData) data).getData()+" "+((RID)
			// ((LeafData) entry.data).getData()));
		}
		// System.out.println("st1:"+st1+" "+st2);
		return (st1 && st2);
	}

}
